from urllib.request import urlretrieve
import socket
import os
import sys

def  reporthook(blocknum, bs, size):
    #  blocknum:已经下载的数据块       bs:数据块的大小      size:远程文件的大小
    per = 100.0 * blocknum * bs / size
    if per > 100 :
        per = 100
    sys.stdout.write('Download progress: %.2f%%  \r'%per)
    sys.stdout.flush()

class DownloadPaper():
    def __init__(self):
        self.url = 'https://nukeprice.blob.core.windows.net/worldjournal'
        #https://nukeprice.blob.core.windows.net/worldjournal/2020-02-02/A01.jpg
        self.sheets=['A','B']
        self.yearStr ='2020'
        self.monStr='2'.zfill(2)
        self.dayStr='1'.zfill(2)
        self.localpath = 'F:\\个人资料\\2020news\\'+self.monStr
        socket.setdefaulttimeout(30)

    def run(self):
        #self.get_paper('A')
        #self.get_paper('B')
        self.get_1mon_paper()

    def get_1mon_paper(self):
        i = 1
        while i <= 31:
            self.dayStr = str(i).zfill(2)
            self.get_1day_paper()    
            i += 1      

    def get_1day_paper(self):   
        dataStr = self.yearStr  + '-%s-%s'%(self.monStr,self.dayStr)
        for sheet in self.sheets:     #['A','B']
            filename = dataStr + '_' + sheet
            url = self.url + '/' + dataStr + '/' + sheet
             
            i = 1        
            while True:
                seq = str(i).zfill(2)
                url1 = url + '%s.jpg'%seq
                localfile = os.path.join(self.localpath,filename+'%s.jpg'%seq)
                print(localfile)
                if os.path.exists(localfile):
                    i += 1
                    continue
                print("Downloading %s"%url1)
                try:
                    urlretrieve(url1,localfile, reporthook= reporthook) 
                except socket.timeout:
                    count = 1
                    while count <= 5:
                        try:
                            urlretrieve(url1,localfile, reporthook= reporthook)                                                
                            break
                        except socket.timeout:
                            err_info = 'Reloading for %d time'%count if count == 1 else 'Reloading for %d times'%count
                            print(err_info)
                            count += 1
                    if count > 5:
                        print("download job failed!")
    
                except Exception as e:
                        print (Exception,":",e)
                        break
                i += 1

if __name__  ==  '__main__':
    DownloadPaper().run()  